package labox.innovation.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import labox.innovation.util.L2Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author bright
 *
 */
public class LoginServerConfig {

    private static final Logger _log = LoggerFactory.getLogger(LoginServerConfig.class.getName());

    private static final String LOGIN_CONFIGURATION_FILE = "./config/loginserver.properties";

    /**
     * 连接服务器所需的最小协议版本号
     */
    public static int MIN_PROTOCOL_REVISION;

    /**
     * 连接服务器所需的最大的协议版本号
     */
    public static int MAX_PROTOCOL_REVISION;

    /**
     * 连接服务器所需的最小客户端版本号
     */
    public static int MIN_CLIENT_REVISION;

    /**
     * 连接服务器所需的最大的客户端版本号
     */
    public static int MAX_CLIENT_REVISION;

    public static String GAME_HOST_NAME;

    /**
     * 登陆服务器外网地址
     */
    public static String LOGIN_BIND_ADDRESS;
    /**
     * 游戏服务器端口
     */
    public static int PORT_GAME;

    /**
     * 登录服务器外网端口
     */
    public static int PORT_LOGIN;
    /**
     * 登陆服务器是否使用socket连接
     */
    public static boolean LOGIN_SOCKET;
    /**
     * 游戏服务器连接登陆服务器的地址（内网地址）
     */
    public static String GAME_SERVER_LOGIN_HOST;

    /**
     * 游戏服务器连接登陆服务器的端口（内网端口）
     */
    public static int GAME_SERVER_LOGIN_PORT;

    /**
     * 服务器内部地址显示在登录服务器下发的列表中的地址
     */
    public static String INTERNAL_HOSTNAME;

    /**
     * 服务器外部地址显示在登录服务器下发的列表中的地址
     */
    public static String EXTERNAL_HOSTNAME;

    /**
     * 路由器地址
     */
    public static String ROUTER_HOSTNAME;
    /**
     * 是否启用debug模式
     */
    public static boolean DEBUG;

    /**
     * 是否启用数据包处理的debug模式
     */
    public static boolean PACKET_HANDLER_DEBUG;
    /**
     * 是否启用开发模式
     */
    public static boolean DEVELOPER;

    /**
     * 是否启用断言模式
     */
    public static boolean ASSERT;

    /**
     * 登陆服务器是否接受新的游戏服务器注册
     */
    public static boolean ACCEPT_NEW_GAMESERVER;

    /**
     * 游戏服务器最终从登陆服务器获取的服务器ID
     */
    public static int SERVER_ID;
    /**
     * 游戏服务器的向登陆服务器请求的唯一标示ID
     */
    public static int REQUEST_ID;

    /**
     * 是否允许服务器的ID被修改
     */
    public static boolean ACCEPT_ALTERNATE_ID;
    /**
     * 是否在日志中记录登陆的情况
     */
    public static boolean LOG_LOGIN_CONTROLLER;

    /**
     * 在被绑前登陆失败的重试次数
     */
    public static int LOGIN_TRY_BEFORE_BAN;

    /**
     * 被绑后解绑的时间（秒）
     */
    public static int LOGIN_BLOCK_AFTER_BAN;
    /**
     * 数据包的根路径
     */
    public static File DATAPACK_ROOT;

    /**
     * 数据库驱动
     */
    public static String DATABASE_DRIVER;

    /**
     * 数据库地址
     */
    public static String DATABASE_URL;

    /**
     * 数据库账户
     */
    public static String DATABASE_LOGIN;

    /**
     * 数据库密码
     */
    public static String DATABASE_PASSWORD;

    /**
     * 数据库最大的连接数
     */
    public static int DATABASE_MAX_CONNECTIONS;

    /**
     * 数据库连接最大的空闲时间
     */
    public static int DATABASE_MAX_IDLE_TIME;

    public static boolean SHOW_LICENCE;

    /**
     * 是否自动创建账户
     */
    public static boolean AUTO_CREATE_ACCOUNTS;

    /**
     * 是否启用注册账户功能
     */
    public static boolean CREATEACCOUNTS;

    /**
     * 登陆服务器账户名正则规则
     */
    public static String ACCOUNT_NAME_TEMPLATE;

    /**
     * 登陆服务器账户的密码正则规则
     */
    public static String ACCOUNT_PASSWORD_TEMPLATE;

    /**
     * 登陆服务器电话号码正则规则
     */
    public static String ACCOUNT_PHONENO_TEMPLATE;
    /**
     * 是否启用登录服务防止游戏服务器的洪水攻击保护
     */
    public static boolean FLOOD_PROTECTION_GAMESERVER;

    public static int FAST_CONNECTION_LIMIT_GAMESERVER;

    public static int NORMAL_CONNECTION_TIME;

    public static int FAST_CONNECTION_TIME;

    public static int MAX_CONNECTION_PER_IP;

    /**
     * 用户登陆超时时间设置，默认一分钟
     */
    public static int LOGIN_TIMEOUT;

    /**
     * 是否启用登录服务防止客户端的洪水攻击保护
     */
    public static boolean FLOOD_PROTECTION_CLIENT;

    public static int FAST_CONNECTION_LIMIT_CLIENT;

    public static boolean RELEASELIST;

    public static String LEARN_SKILL_LVL;

    /**
    * 
    */
    public static void load() {
        _log.info("Loading loginServer Configuration Files...", Config.SERVER_ID);
        InputStream is = null;
        try {
            L2Properties serverSettings = new L2Properties();
            is = new FileInputStream(new File(LOGIN_CONFIGURATION_FILE));
            serverSettings.load(is);

            MIN_PROTOCOL_REVISION = Integer.parseInt(serverSettings.getProperty("MinProtocolRevision", "40"));
            MAX_PROTOCOL_REVISION = Integer.parseInt(serverSettings.getProperty("MaxProtocolRevision", "40"));

            LOGIN_BIND_ADDRESS = serverSettings.getProperty("LoginserverHostname", "*");
            PORT_LOGIN = Integer.parseInt(serverSettings.getProperty("LoginserverPort", "2106"));
            LOGIN_SOCKET = Boolean.parseBoolean(serverSettings.getProperty("LoginSocket", "false"));

            GAME_SERVER_LOGIN_HOST = serverSettings.getProperty("LoginHostname", "*");
            GAME_SERVER_LOGIN_PORT = Integer.parseInt(serverSettings.getProperty("LoginPort", "9014"));

            DEBUG = Boolean.parseBoolean(serverSettings.getProperty("Debug", "false"));
            PACKET_HANDLER_DEBUG = Boolean.parseBoolean(serverSettings.getProperty("PacketHandlerDebug", "false"));
            DEVELOPER = Boolean.parseBoolean(serverSettings.getProperty("Developer", "false"));
            ASSERT = Boolean.parseBoolean(serverSettings.getProperty("Assert", "false"));

            ACCEPT_NEW_GAMESERVER = Boolean.parseBoolean(serverSettings.getProperty("AcceptNewGameServer", "True"));
            SERVER_ID = REQUEST_ID = Integer.parseInt(serverSettings.getProperty("RequestServerID", "0"));
            ACCEPT_ALTERNATE_ID = Boolean.parseBoolean(serverSettings.getProperty("AcceptAlternateID", "True"));

            LOG_LOGIN_CONTROLLER = Boolean.parseBoolean(serverSettings.getProperty("LogLoginController", "true"));
            LOGIN_TRY_BEFORE_BAN = Integer.parseInt(serverSettings.getProperty("LoginTryBeforeBan", "10"));
            LOGIN_BLOCK_AFTER_BAN = Integer.parseInt(serverSettings.getProperty("LoginBlockAfterBan", "600"));

            DATAPACK_ROOT = new File(serverSettings.getProperty("DatapackRoot", ".")).getCanonicalFile();

            INTERNAL_HOSTNAME = serverSettings.getProperty("InternalHostname", "localhost");
            EXTERNAL_HOSTNAME = serverSettings.getProperty("ExternalHostname", "localhost");
            ROUTER_HOSTNAME = serverSettings.getProperty("RouterHostname", "");

            DATABASE_DRIVER = serverSettings.getProperty("Driver", "com.mysql.jdbc.Driver");
            DATABASE_URL = serverSettings.getProperty("URL", "jdbc:mysql://localhost/inndb");
            DATABASE_LOGIN = serverSettings.getProperty("Login", "root");
            DATABASE_PASSWORD = serverSettings.getProperty("Password", "root");
            DATABASE_MAX_CONNECTIONS = Integer.parseInt(serverSettings.getProperty("MaximumDbConnections", "10"));
            DATABASE_MAX_IDLE_TIME = Integer.parseInt(serverSettings.getProperty("MaximumDbIdleTime", "0"));

            SHOW_LICENCE = Boolean.parseBoolean(serverSettings.getProperty("ShowLicence", "true"));
            AUTO_CREATE_ACCOUNTS = Boolean.parseBoolean(serverSettings.getProperty("AutoCreateAccounts", "True"));
            CREATEACCOUNTS = Boolean.parseBoolean(serverSettings.getProperty("CreateAccounts", "True"));
            ACCOUNT_NAME_TEMPLATE = serverSettings.getProperty("AnameTemplate", ".*");
            ACCOUNT_PASSWORD_TEMPLATE = serverSettings.getProperty("ApasswordTemplate", ".*");
            ACCOUNT_PHONENO_TEMPLATE = serverSettings.getProperty("AphoneNoTemplate", ".*");

            FLOOD_PROTECTION_GAMESERVER = Boolean.parseBoolean(serverSettings.getProperty("EnableFloodProtectionGameServer", "True"));
            FAST_CONNECTION_LIMIT_GAMESERVER = Integer.parseInt(serverSettings.getProperty("FastConnectionLimitGameServer", "15"));
            NORMAL_CONNECTION_TIME = Integer.parseInt(serverSettings.getProperty("NormalConnectionTime", "700"));
            FAST_CONNECTION_TIME = Integer.parseInt(serverSettings.getProperty("FastConnectionTime", "350"));
            MAX_CONNECTION_PER_IP = Integer.parseInt(serverSettings.getProperty("MaxConnectionPerIP", "50"));
            LOGIN_TIMEOUT = Integer.parseInt(serverSettings.getProperty("LoginTimeOut", "60000"));
            FLOOD_PROTECTION_CLIENT = Boolean.parseBoolean(serverSettings.getProperty("EnableFloodProtectionClient", "True"));
            FAST_CONNECTION_LIMIT_CLIENT = Integer.parseInt(serverSettings.getProperty("FastConnectionLimitClient", "20"));
        } catch (Exception e) {
            _log.error("Failed to Load " + LOGIN_CONFIGURATION_FILE + " File.", e);

        } finally {
            try {
                is.close();
            } catch (IOException e) {
                _log.error("InputStream close occured IOException");
            }
        }
    }
}
